package jdk9;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;

/**
 * JDK 9 新特性：try-with-resources 增强
 * <p>
 * try-with-resources 是 Java 7 引入的新特性
 * JDK 9 对 try-with-resources 进行了增强
 */
public class TryWithResourcesDemo {
    /**
     * 文件路径
     */
    public static final String FILE_PATH = "D:\\file\\my\\study\\demo.txt";

    /**
     * JDK 6 写法
     */
    public static void printTextByJDK6() {
        FileOutputStream fileOutputStream = null;
        FileInputStream fileInputStream = null;

        try {
            fileOutputStream = new FileOutputStream(FILE_PATH);
            fileInputStream = new FileInputStream(FILE_PATH);

            // 写
            fileOutputStream.write("hello java".getBytes(Charset.defaultCharset()));

            // 读取
            int data = fileInputStream.read();
            while (data != -1) {
                System.out.print((char) data);
                data = fileInputStream.read();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭写流
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            // 关闭读流
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * JDK 7 写法
     */
    public static void printTextByJDK7() throws Exception {
        try (
                FileOutputStream fileOutputStream = new FileOutputStream(FILE_PATH);
                FileInputStream fileInputStream = new FileInputStream(FILE_PATH)
        ) {
            // 写
            fileOutputStream.write("hello java".getBytes(Charset.defaultCharset()));

            // 读取
            int data = fileInputStream.read();
            while (data != -1) {
                System.out.print((char) data);
                data = fileInputStream.read();
            }
        }
    }

    /**
     * JDK 9 写法
     * 在 Java 9 之后，在 try-with-resources 语句中可以使用 effectively-final 变量。
     * effectively-final 变量：简单来说就是没有被 final 修饰但是值在初始化后从未更改的变量。
     */
    public static void printTextByJDK9() throws Exception {
        FileOutputStream fileOutputStream = new FileOutputStream(FILE_PATH);
        FileInputStream fileInputStream = new FileInputStream(FILE_PATH);
        try (fileOutputStream; fileInputStream) {
            // 写
            fileOutputStream.write("hello java".getBytes(Charset.defaultCharset()));

            // 读取
            int data = fileInputStream.read();
            while (data != -1) {
                System.out.print((char) data);
                data = fileInputStream.read();
            }
        }
    }

    public static void main(String[] args) throws Exception {
        printTextByJDK6();
        printTextByJDK7();
        printTextByJDK9();
    }
}
